package com.endava.nio_io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class IO_ObjectStream {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {

		File f = new File("square.dat");
		if (f.exists() == false) {
			f.createNewFile();
		} else {
//			System.out.println(f);
		}

		Square square = new Square(10);// create square object

		FileOutputStream fos = new FileOutputStream("square.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(square);// write this square object to disk
		oos.writeObject(new Date());// write today's date to disk
		oos.close();// close file

		FileInputStream fis = new FileInputStream("square.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		Square mySquare = (Square) ois.readObject();// read square from disk
		Date today = (Date) ois.readObject();// read date object from disk
		ois.close();// close file

		System.out.println(mySquare.getLine());
		System.out.println(mySquare);
		System.out.println(today);
	}

}
